<?php
namespace Admin\Controller;
use Think\Controller;


/**
* 
*/
class AdminController extends BaseController
{
	
	public function index()
	{
		// $sql = "
		// 	SELECT
		// 		a.id, a.username, a.reg_time, r.name AS role_name, r.remark 
		// 	FROM
		// 		jy_admin AS a
		// 	LEFT JOIN jy_role AS r ON a.role_id = r.role_id
		// 	";
		// $list = M()->query($sql);
		// $this->assign('list', $list);
		$this->display();
	}

	public function admin_info()
	{
		// $admin_id = I('get.id',0);   	
		// if($admin_id){
		// 	$info = M('admin')->where("id=$admin_id")->find();
		// 	$info['password'] =  "";
		// 	$this->assign('info',$info);
		// }
		// $act = empty($admin_id) ? 'add' : 'edit';
		// $this->assign('act',$act);
		// $role = M('role')->where('1=1')->select();
		// $this->assign('role', $role);
		$this->display();
	}


	public function adminHandle()
	{
		$data = I('post.');
		if(empty($data['password']))
		{
			unset($data['password']);
		}
		else
		{
			$data['password'] = strtoupper(md5($data['password']));
		}
		if($data['act'] == 'add')
		{
			unset($data['admin_id']);
			$data['reg_time'] = time();
			if(M('admin')->where("username='".$data['username']."'")->count())
			{
				returnJsonStr(1, '此用户名已被注册，请更换');
			}
			else
			{
				$r = M('admin')->add($data);
			}
		}
		
		if($data['act'] == 'edit')
		{
			// $data['re_time'] = time();
			$r = M('admin')->where('id='.$data['admin_id'])->save($data);
		}
		
	    if($data['act'] == 'del' && $data['admin_id']>1)
	    {
			$r = M('admin')->where('id='.$data['admin_id'])->delete();
		}
		
		if($r)
		{
			returnJsonStr(0, '操作成功！');
		}
		else
		{
			returnJsonStr(1, '操作失败！');
		}
	}


	public function role()
	{
		// $sql = "select * from jy_role order by role_id desc";
		// // $sql = "select * from jy_admin where level < 1 order by role_id desc";
		// $list = M()->query($sql);
		// $this->assign('list', $list);
		// $this->assign('empty', "<p class='empty'>无数据</p>");
		$this->display();
	}

	public function role_info()
	{
		// $role_id = I('get.role_id');
		// if($role_id)
		// {
		// 	$detail = M('role')->where(array('role_id'=>$role_id))->find();
		// 	$detail['pid_list'] = explode(',', $detail['pid_list']);
		// 	$this->assign('detail',$detail);
		// }
		// $sql = "select * from jy_permission";
		// $list = M()->query($sql);

		// foreach($list as $key => $val)
		// {
		// 	if(!empty($detail))
		// 	{
		// 		$val['enable'] = in_array($val['id'], $detail['pid_list']);
		// 	}
		// }
		// $tree = getTree($list);
		// $this->assign('list', $tree);
		$this->display();
	}

	public function roleSave()
	{
		$data = I('post.');
    	$res = $data['data'];
    	$res['pid_list'] = is_array($data['right']) ? implode(',', $data['right']) : '';
    	
    	if(empty($data['role_id'])){
    		if(M('role')->where(array('name'=>$res['name']))->find())
	    	{
	    		returnJsonStr(0,"该角色已存在");
	    	}
	    	$res['add_time'] = time();
    		$r = M('role')->add($res);
    	}else{
    		$res['re_time'] = time();
    		$r = M('role')->where('role_id='.$data['role_id'])->save($res);
    	}
		if($r){
			returnJsonStr(1,"操作成功");
		}else{
			returnJsonStr(0,"操作失败");
		}
	}


	public function del_role()
	{
		$id = $_GET['role_id'];
		if(!is_numeric($id))
		{
			returnJsonStr(0,'参数错误');
		}

		$sql = "delete from jy_role where role_id = $id";
		$r = M()->execute($sql);

		if($r)
		{
			returnJsonStr(1,"操作成功");
		}
		else
		{
			returnJsonStr(0,"操作失败");
		}
	}

}
